package com.station.search.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

/**
 * @author: JT
 * @date: 2021/9/1
 * @Title:
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Log4j2
public class SwaggerConfig {

    @Value("${spring.profiles.active:''}")
    private String active;
    @Value("${server.port}")
    private int port;
    @Value("${server.servlet.context-path:}")
    private String contextPath;

    @Bean
    public Docket createRestApi() throws UnknownHostException {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(!active.equalsIgnoreCase("deploy"))
                .groupName("1.0")
                .globalResponseMessage(RequestMethod.GET, globalResponse())
                .globalResponseMessage(RequestMethod.POST, globalResponse())
                .globalResponseMessage(RequestMethod.PUT, globalResponse())
                .globalResponseMessage(RequestMethod.DELETE, globalResponse())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
        String ipAddress = InetAddress.getLocalHost().getHostAddress();

        log.info("Swagger2接口文档地址: http://{}:{}{}/swagger-ui.html", ipAddress, port, contextPath);
        log.info("Knife4j增强接口文档地址: http://{}:{}{}/doc.html", ipAddress, port, contextPath);
        return docket;

    }

    private List<ResponseMessage> globalResponse() {
        return Arrays.asList(
                new ResponseMessageBuilder().code(401).message("未认证").build(),
                new ResponseMessageBuilder().code(403).message("请求被禁止").build(),
                new ResponseMessageBuilder().code(404).message("找不到资源").build(),
                new ResponseMessageBuilder().code(500).message("服务器内部错误").build()
        );
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("台站信息检索系统")
                .description("台站信息检索系统API接口文档")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}
